/*
 * File:   CoordinatesInverterTest.cpp
 * Author: Guilherme Monteiro
 *
 */

#include "CoordinatesInverterTest.h"

CPPUNIT_TEST_SUITE_REGISTRATION(CoordinatesInverterTest);

const int TAX_TOLERANCE = 50; // 50 pixels of tolerance

CoordinatesInverterTest::CoordinatesInverterTest() {
}

CoordinatesInverterTest::~CoordinatesInverterTest() {
}

void CoordinatesInverterTest::setUp() {
}

void CoordinatesInverterTest::tearDown() {
}

/*
 * Simple C++ Test Suite
 */

void CoordinatesInverterTest::testInvertPoints1() {
    string testPathToImageNotCorrected = "../books/caracterizacao/baixa_resolucao/image133_t.png";  // not corrected
    string testPathToImageCorrected = "../books/caracterizacao/baixa_resolucao/image133_t_r.png"; // corrected
    
    /**
     * coordinates of image not corrected,
     * this points will be corrected to in put in
     * image of testPath2
     */
    vector<CvPoint> coordinates;  
    coordinates.push_back(cvPoint(252, 272));
    coordinates.push_back(cvPoint(2180,3084));
    
    IplImage* imgNotCorrected = cvLoadImage(testPathToImageNotCorrected.c_str());
    IplImage* imgCorrected = cvLoadImage(testPathToImageCorrected.c_str()); 
    
//    printf("h rotacionada: %d\n", img->height);
//    printf("w rotacionada: %d\n", img->width);
//    printf("h nao rotacionada: %d\n", img2->height);
//    printf("w nao rotacionada: %d\n", img2->width);
    
    /**
     * receives the image ready corrected to invert the coordinates of roi
     */
    CoordinatesInverter *coordinatesInverter = new CoordinatesInverter(coordinates, imgCorrected);
    vector<CvPoint> newPoints = coordinatesInverter->invertPoints();
    
    CvPoint newLU = newPoints[0];
    CvPoint newRB = newPoints[1];
    
    CvPoint LUInvByUser = cvPoint(300,250);
    CvPoint RBInvByUser = cvPoint(3126,2178);
    
    bool result = 
    (LUInvByUser.x - TAX_TOLERANCE <= newLU.x && LUInvByUser.x + TAX_TOLERANCE >= newLU.x) &&
    (LUInvByUser.y - TAX_TOLERANCE <= newLU.y && LUInvByUser.y + TAX_TOLERANCE >= newLU.y) &&
    (RBInvByUser.x - TAX_TOLERANCE <= newRB.x && RBInvByUser.x + TAX_TOLERANCE >= newRB.x) &&
    (RBInvByUser.y - TAX_TOLERANCE <= newRB.y && RBInvByUser.y + TAX_TOLERANCE >= newRB.y);
    
    cvRectangle(imgCorrected, newLU, newRB, CV_RGB(255,0,0));
    
//    cvNamedWindow("image with rectangle", CV_WINDOW_FREERATIO);
//    cvShowImage("image with rectangle" , imgCorrected);
//    cvWaitKey(0);
//    cvDestroyAllWindows();
    
    if (!result) {
        CPPUNIT_ASSERT(false);
    }
    
    cvReleaseImage(&imgNotCorrected);
    cvReleaseImage(&imgCorrected);
}

void CoordinatesInverterTest::testInvertPoints2() {
    string testPathToImageNotCorrected = "../books/caracterizacao/baixa_resolucao/image81_t.png"; // nao rotacionada pelo sistema
    string testPathToImageCorrected = "../books/caracterizacao/baixa_resolucao/image81_t_r.png";  // rotacionada pelo sistema
    
    vector<CvPoint> coordinates;
    coordinates.push_back(cvPoint(477,240)); // LU
    coordinates.push_back(cvPoint(3093,2304)); // RB
    
    // rotated rightly image
    IplImage* imgNotCorrected = cvLoadImage(testPathToImageNotCorrected.c_str()); 
    IplImage* imgCorrected = cvLoadImage(testPathToImageCorrected.c_str()); 
    
//    printf("h rotacionada: %d\n", img->height);
//    printf("w rotacionada: %d\n", img->width);
//    printf("h nao rotacionada: %d\n", img2->height);
//    printf("w nao rotacionada: %d\n", img2->width);
    
    /**
     * receives the image ready corrected to invert the coordinates of roi
     */
    CoordinatesInverter *coordinatesInverter = new CoordinatesInverter(coordinates, imgCorrected);
    vector<CvPoint> newPoints = coordinatesInverter->invertPoints();
    
    CvPoint newLU = newPoints[0];
    CvPoint newRB = newPoints[1];
    
    CvPoint LUInvByUser = cvPoint(72,477);
    CvPoint RBInvByUser = cvPoint(2136,3093);
    
    bool result = 
    (LUInvByUser.x - TAX_TOLERANCE <= newLU.x && LUInvByUser.x + TAX_TOLERANCE >= newLU.x) &&
    (LUInvByUser.y - TAX_TOLERANCE <= newLU.y && LUInvByUser.y + TAX_TOLERANCE >= newLU.y) &&
    (RBInvByUser.x - TAX_TOLERANCE <= newRB.x && RBInvByUser.x + TAX_TOLERANCE >= newRB.x) &&
    (RBInvByUser.y - TAX_TOLERANCE <= newRB.y && RBInvByUser.y + TAX_TOLERANCE >= newRB.y);
    
    cvRectangle(imgCorrected, newLU, newRB, CV_RGB(255,0,0));
//    
//    cvNamedWindow("image with rectangle", CV_WINDOW_FREERATIO);
//    cvShowImage("image with rectangle" , imgCorrected);
//    cvWaitKey(0);
//    cvDestroyAllWindows();
    
    if (!result) {
        CPPUNIT_ASSERT(false);
    }
    
    cvReleaseImage(&imgNotCorrected);
    cvReleaseImage(&imgCorrected);
}


 